using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace ModelAPI{
    // 水类
    public class WaterModel{
        GameObject water;
        public WaterModel(){
            water=Object.Instantiate(Resources.Load<GameObject>("Prefabs/Water")) as GameObject;
            water.name="water";
        }
    }
    // 陆地类
    public class LandModel{
        GameObject land;
        int land_side;          //陆地方向
        Vector3[] positions;    //陆地上座位的坐标
        RoleModel[] roles = new RoleModel[6];   

        public LandModel(int side){
            positions=new Vector3[] {
                new Vector3(2.5f,-1.75f,0), new Vector3(3.25f,-1.75f,0), new Vector3(4,-1.75f,0),
                new Vector3(4.75f,-1.75f,0), new Vector3(5.5f,-1.75f,0), new Vector3(6.25f,-1.75f,0)
            };
            land_side = side;
            land=Object.Instantiate(Resources.Load<GameObject>("Prefabs/Land"+((side==1)?"Start":"End"))) as GameObject;
            land.name ="Land"+((side==1)?"Start":"End");
        }

        public int getSide(){
            return land_side;
        }
        public int getEmptyNumber(){
            for(int i=0;i<6;i++){
                if(roles[i]==null)
                    return i;
            }
            return -1;
        }
        public Vector3 getEmptyPos(){
            Vector3 res=positions[getEmptyNumber()];
            res.x*=land_side;
            return res;
        }
        public int[] getRoleNum(){
            int[] res={0,0};
            for(int i=0;i<6;i++){
                if(roles[i]!=null){
                    if(roles[i].getSign()==0) res[0]++;
                    else res[1]++;
                }
            }
            return res;
        }

        public void addRole(RoleModel role){
            roles[getEmptyNumber()]=role;
        }
        public RoleModel deleteRole(string name){
            for(int i=0;i<6;i++){
                if(roles[i]!=null && roles[i].getName()==name){
                    RoleModel res=roles[i];
                    roles[i]=null;
                    return res;
                }
            }
            return null;
        }
    }

    //船类，需要点击和移动事件
    public class BoatModel{
        GameObject boat;
        int boat_side;  //船在哪一边
        // Move move;
        Click click;
        Vector3[] startPos,endPos;  //船上的座位坐标
        RoleModel[] roles=new RoleModel[2];

        public BoatModel(){
            boat=Object.Instantiate(Resources.Load<GameObject>("Prefabs/Boat")) as GameObject;
            boat_side=1;
            startPos=new Vector3[]{ new Vector3(0.9f,-1.75f,0), new Vector3(1.65f,-1.75f,0) };
            endPos = new Vector3[]{ new Vector3(-1.65f,-1.75f,0), new Vector3(-0.9f,-1.75f,0) };
            // move=boat.AddComponent(typeof(Move)) as Move;
            click=boat.AddComponent(typeof(Click)) as Click;
            click.setBoat(this);
            boat.name="boat";
        }
        public int getSide(){
            return boat_side;
        }
        public int getEmptyNumber(){
            for(int i=0;i<2;i++){
                if(roles[i]==null)
                    return i;
            }
            return -1;
        }
        public Vector3 getEmptyPos(){
            Vector3 res;
            res=(boat_side==1)?startPos[getEmptyNumber()]:endPos[getEmptyNumber()];
            return res;
        }
        public int[] getRoleNum(){
            int[] res={0,0};
            for(int i=0;i<2;i++){
                if(roles[i]!=null){
                    if(roles[i].getSign()==0) res[0]++;
                    else res[1]++;  
                }
            }
            return res;
        }

        public void addRole(RoleModel role){
            roles[getEmptyNumber()]=role;
        }
        public RoleModel deleteRole(string name){
            for(int i=0;i<2;i++){
                if(roles[i]!=null && roles[i].getName()==name){
                    RoleModel res=roles[i];
                    roles[i]=null;
                    return res;
                }
            }
            return null;
        }
        public void changeSide(){
            boat_side*=-1;
        }
        // public void boatMove(){
        //     if(boat_side==-1) move.MovePosition(new Vector3(1.25f,-2.75f,0),0.5f);
        //     else move.MovePosition(new Vector3(-1.25f,-2.75f,0),0.5f);
        //     boat_side=-boat_side;
        // }

        public GameObject getBoat(){
            return boat;
        }

        public bool empty(){
            return (roles[0]==null)&&(roles[1]==null);
        }
    }

    // 人物类
    public class RoleModel{
        GameObject role;
        int roleSign;   //人物种类，1表示牧师，-1表示魔鬼
        int onBoat;     //是否在船上
        int land_side;  //在哪边的陆地上
        // Move move;
        Click click;

        public RoleModel(string name,Vector3 pos){
            if(name=="priest") {
                role=Object.Instantiate(Resources.Load<GameObject>("Prefabs/Priest"),
                    pos, Quaternion.identity) as GameObject;
                roleSign=0;
            }else{
                role=Object.Instantiate(Resources.Load<GameObject>("Prefabs/Devil"),
                    pos, Quaternion.identity) as GameObject;
                roleSign=1;
            }
            land_side=1;
            // move=role.AddComponent(typeof(Move)) as Move;
            click=role.AddComponent(typeof(Click)) as Click;
            click.setRole(this);
        }

        public string getName(){ 
            return role.name; 
        }
        public int getSide(){ 
            return land_side; 
        }
        public void setSide(int s){ 
            land_side=s; 
        }
        public int getSign(){
            return roleSign;
        }
        public int getOnBoat(){ 
            return onBoat; 
        }
        public void setOnBoat(int a){ 
            onBoat=a; 
        }
        public void setName(string name){ 
            role.name=name; 
        }
        public void setPosition(Vector3 pos) {
            role.transform.position = pos;
        }
        public GameObject getRole(){
            return role;
        }
        // public void roleMove(Vector3 pos){
        //     move.MovePosition(pos,1f);
        // }
    }
}